package juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//精准调用
//1买好》3买》2买
public class Demo5_JUC {
    public static void main(String[] args) {
        Goods5 goods5 = new Goods5();


        new Thread(()->{
            //店家线程
            System.out.println("店家线程：");
            //增加多个商品
            goods5.addThisGoods();
            System.out.println("店家新增好了！");
        },"restaurant").start();

        new Thread(()->{
            //顾客线程
            System.out.println("顾客线程1：");
            goods5.buyThisGoodsA();
            System.out.println("顾客1买好A了！");
        },"customer1").start();

        new Thread(()->{
            //顾客线程
            System.out.println("顾客线程2：");
            goods5.buyThisGoodsC();
            System.out.println("顾客2买好了C！");
        },"customer2").start();

        new Thread(()->{
            //顾客线程
            System.out.println("顾客线程3：");
            goods5.buyThisGoodsB();
            System.out.println("顾客3买好了B！");
        },"customer3").start();

    }
}
class Goods5{
    private String name = "商品1";
    /**
     * 库存
     */
    private int count = 0;

    Lock lock = new ReentrantLock();

    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();


    public void buyThisGoodsA() {
        //加锁
        lock.lock();

        try {
            while (count!=3){
                condition1.await();
            }

            count--;
            System.out.println("buyThisGoodsA: " + Thread.currentThread().getName() + "=>" + name + ":" + count);
            condition3.signal();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //解锁
            lock.unlock();
        }

    }

    public void buyThisGoodsB() {
        //加锁
        lock.lock();

        try {
            while (count!=1){
                condition2.await();
            }

            count--;
            System.out.println("buyThisGoodsB: " + Thread.currentThread().getName() + "=>" + name + ":" + count);
            condition1.signal();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //解锁
            lock.unlock();
        }

    }

    /**
     * C用户购买
     */
    public void buyThisGoodsC() {
        //加锁
        lock.lock();

        try {
            while (count!=2){
                condition3.await();
            }

            count--;
            System.out.println("buyThisGoodsC: " + Thread.currentThread().getName() + "=>" + name + ":" + count);
            condition2.signal();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //解锁
            lock.unlock();
        }


    }

    /**
     * 店家添加库存
     */
    public void addThisGoods(){
        lock.lock();
        for(int i= 0 ; i< 3 ; i++){
            count++;
        }
        System.out.println("addThisGoods: " + Thread.currentThread().getName() + "=>" + name + ":" + count);
        condition1.signal();
        lock.unlock();
    }
}